package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import model.User;
import service.UserServiceImpl;

public class LoginDialog extends JDialog implements ActionListener{
	JButton jbtlogin,jbtinsert;
	JLabel jlbuserid,jlbpassword;
	JTextField jtfuserid,jtfpassword;
	Check check;
	User user;
           LoginDialog(){
        	
        	   	setTitle("用户服务");
        	   	setSize(350,150);
        	   	setLocationRelativeTo(null);//居中
        	   	setResizable(false);
        	   	setLayout(null);
        	   	jlbuserid=new JLabel("用户账号");
        	   	jlbpassword=new JLabel("用户密码");
        	  
        	   	jtfuserid=new JTextField(30);
        	   	jtfpassword=new JTextField(30);
        	   
        	   	jbtlogin=new JButton("登陆");      	
        	   	jbtinsert=new JButton("注册");
        	   	
        	   	jlbpassword.setBounds(185, 20, 65, 30);add(jlbpassword);  
        	   	jlbuserid.setBounds(20, 20, 65, 30);   add(jlbuserid);
        	   	
        	   	jtfuserid.setBounds(75, 20, 100, 30);    add(jtfuserid);
        	   	jtfpassword.setBounds(240, 20	,100, 30);  add(jtfpassword);
        	
        	   	
        	   	jbtlogin.setBounds(80,70 , 80, 25);  add(jbtlogin);  jbtlogin.addActionListener(this);        	   
        	   	jbtinsert.setBounds(180,70,80,25);add(jbtinsert);  jbtinsert.addActionListener(this);
        	   	setVisible(true);
           }
           public void actionPerformed(ActionEvent e){
        	   UserServiceImpl temp=new UserServiceImpl();
        	   
        	   if(e.getSource()==jbtlogin){
        		   dispose();
        	
        	   check=new Check();
        	   try{
        	   if(check.CheckPassword(Integer.parseInt(jtfuserid.getText().trim()),jtfpassword.getText().trim())){
        		   try {
        			   user.setUserid(Integer.parseInt(jtfuserid.getText().trim()));
        			   user.setPassword(jtfpassword.getText().trim());
					new MainFrame(user);
				} catch ( Exception e1) {
					e1.printStackTrace();
				}
        	   }
        	   else
        	   {
        		   JOptionPane.showMessageDialog(null, "用户不存在或密码错误", "警告",
							JOptionPane.NO_OPTION);
        	   }
        	   }
        	   catch(Exception e1){
        		   e1.printStackTrace();
        	   }}

        	   if(e.getSource() == jbtinsert){
        		   dispose();
        		new CreateDialog();
        	   }
           }
      
}
